﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class CarInfoBLL
    {
        #region 构造单例
        private CarInfoBLL() { }
        private static CarInfoBLL _instance;
        public static CarInfoBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new CarInfoBLL());
            }
        }
        #endregion

        public int Add(Carinfo info) 
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Carinfo.Where(p => p.CarNo == info.CarNo).FirstOrDefault();
                if (exist != null)
                {
                    return -2;
                }
                ef.Carinfo.Add(info);
                ef.Entry<Carinfo>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public int Update(Carinfo info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Carinfo.Where(p => p.CarId == info.CarId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else 
                {
                    int isUpdate = ef.Carinfo.Where(p => p.CarNo == info.CarNo && p.CarId != info.CarId).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                }
                exist.AliasName = info.AliasName;
                exist.CarName = info.CarName;
                exist.CarNo = info.CarNo;
                exist.DeliveryTime = info.DeliveryTime;
                exist.IsEnable = info.IsEnable;
                exist.Brand = exist.Brand;
                ef.Carinfo.Add(exist);
                ef.Entry<Carinfo>(exist).State = EntityState.Modified;
                return ef.SaveChanges();
            }
        }

        public int Remove(int carId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Carinfo.Where(p => p.CarId == carId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }

                //移除配置信息
                var existGpsDeviceConfigList = ef.Gpsdeviceconfig.Where(p => p.BindId == exist.CarId && p.BindType == (int)GpsBindTypeEnum.Car).ToList();
                foreach (var item in existGpsDeviceConfigList)
                {
                    ef.Gpsdeviceconfig.Add(item);
                    ef.Entry<Gpsdeviceconfig>(item).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
                ef.Carinfo.Add(exist);
                ef.Entry<Carinfo>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();
            }
        }

        public List<CarInfoModel> GetByPage(int? isEnable, RequestPageBase page)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.GpsDeviceID,c.GpsDeviceName,c.GpsDeviceNo from carinfo as a ";
                sql += "  left join gpsdeviceconfig  as b on a.CarID =b.BindID  ";
                sql += "  left join gpsdevice as c on b.GpsDeviceID=c.GpsDeviceID  ";
                sql += $" where 1=1  ";
                sql += " {0} ";
                string paramsSql = string.Empty;
                if (isEnable.HasValue)
                {
                    paramsSql += $" and a.IsEnable = {isEnable}";
                }
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += $"  a.CarNo like '%{page.Keyword}%'";
                    paramsSql += $" or a.CarName like '%{page.Keyword}%'";
                    paramsSql += $" or a.AliasName like '%{page.Keyword}%'";
                    paramsSql += $" or c.GpsDeviceName like '%{page.Keyword}%'";
                    paramsSql = $" and ({paramsSql})";
                }
                sql = string.Format(sql, paramsSql);
                var countSql = sql.CountSql();
                page.TotalRows = Convert.ToInt32(ef.Database.SqlQuery(countSql).Rows[0][0]);
                paramsSql += page.OrderByPattern == 0 ? "a.CreateTime".OrderByDescending() : "a.CreateTime".OrderBy();
                paramsSql += page.Limit();
                var list = ef.Database.SqlQuery<CarInfoModel>(sql);
                return list;
            }
        }
    }
}
